package com.central.scheduler.worker.processor;

import com.central.scheduler.common.enums.ExecutionStatus;
import com.central.scheduler.common.utils.JSONUtils;
import com.central.scheduler.remote.command.Command;
import com.central.scheduler.remote.command.CommandType;
import com.central.scheduler.remote.command.DBTaskResponseCommand;
import com.central.scheduler.remote.processor.NettyRequestProcessor;
import com.central.scheduler.worker.cache.ResponceCache;
import com.google.common.base.Preconditions;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Tindy
 * @date 2021/10/29
 * @describe
 *  db task response processor
 */
public class DBTaskResponseProcessor implements NettyRequestProcessor {

    private final Logger logger = LoggerFactory.getLogger(DBTaskResponseProcessor.class);

    @Override
    public void process(Channel channel, Command command) {
        Preconditions.checkArgument(CommandType.DB_TASK_RESPONSE == command.getType(),
                String.format("invalid command type : %s", command.getType()));

        DBTaskResponseCommand taskResponseCommand = JSONUtils.parseObject(
                command.getBody(), DBTaskResponseCommand.class);

        if (taskResponseCommand == null){
            return;
        }

        if (taskResponseCommand.getStatus() == ExecutionStatus.SUCCESS.getCode()){
            ResponceCache.get().removeResponseCache(taskResponseCommand.getTaskInstanceId());
        }
    }


}
